package com.humi.partner.manager.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.Converter;

import com.alibaba.druid.util.StringUtils;

import lombok.extern.slf4j.Slf4j;
/**
 * 
 * <pre>
 * <b>.</b>
 * <b>Description:自定义字段转换方法</b> 
 *    
 * <b>Author:</b> tianjunmin@360humi.com
 * <b>Date:</b> 2019年7月26日 上午11:53:37
 * <b>Copyright:</b> Copyright 2017-2019 www.360humi.com Technology Co., Ltd. All rights reserved.
 * <b>Changelog:</b>
 *   Ver   		Date                    Author               	 Detail
 *   ----------------------------------------------------------------------
 *   1.0   2019年7月26日 上午11:53:37    tianjunmin@360humi.com     new file.
 * </pre>
 */
@Slf4j
public class CustomDateConverter implements Converter{

	@SuppressWarnings("rawtypes")
	@Override
	public Object convert(Class type, Object value) {
		Date date = null;
		log.error(type.toString());
		if (value instanceof String &&!StringUtils.isEmpty(value+"")) {
			String dateStr = (String) value;
			String format = "yyyyMMdd";
			if (dateStr.indexOf('-') > 0) {
				format = "yyyy-MM-dd";
			} else if (dateStr.indexOf('/') > 0) {
				format = "yyyy/MM/dd";
			}else if(dateStr.length()==4) {
				format = "yyyy";
			}
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			try {
				date = sdf.parse(dateStr);
			} catch (ParseException e) {
				log.error(e.getMessage());
			}

		}else if(value instanceof Date) {
			date=(Date)value;
		}
		return date;
	}

}
